package com.itlong.cloud.utils.encrypt;

import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;

import java.nio.charset.Charset;
import java.util.Date;
import java.util.Random;

/**
 * <desc>
 *      一卡通加密协议工具类
 * </desc>
 *
 * @createDate2020/9/24
 **/
public class SmartCardEncryptUtil {


    /**
     * <desc>
     *      加密：
     * 1、 将一个字符串的各位的ASCII值转换为2位16进制数，然后连接起来。用于中文转16进制数。
     * </desc>
     *
     * @param asciiStr
     * @return
     * @Author Zhu.yj
     * @createDate  2020/9/24
     */
    public static String asciiStrToHexStr(String asciiStr) {
        String hexStr = "";
        byte[] bytes = asciiStr.getBytes(Charset.forName("gb2312"));
        for (int i = 0; i < bytes.length; i++) {
            //java中长度超过127的时候会变成负数，所以需要保证得到的长度是正数byte b & 0xFF
            hexStr += FormatStrLen(Integer.toHexString(bytes[i] & 0xFF), "0", 2, false, true);
        }
        return hexStr.toUpperCase();
    }

    /**
     * <desc>
     *      证书加密
     * </desc>
     *
     * @param str
     * @return
     * @Author Zhu.yj
     * @createDate  2020/10/12
     */
    public static String LicenseEncrypt(String str) {                // 将包含中文的字符串转换为16进制字符串
        str = asciiStrToHexStr(str);
        if (StringUtils.isBlank(str)) {
            return "";
        }
        byte[] bytes = str.getBytes();
        StringBuilder sb = new StringBuilder();
        Integer[] btRan = new Integer[] { 0x8b00, 0x5300, 0x6700, 0x2c00, 0x5b00, 0x6a00, 0x7d00, 0x3e00 };
        String[] base64Ran = new String[] { "A", "B", "C", "D", "E", "a", "b", "c", "d", "e" };
        Random random = new Random();
        random.setSeed(new Date().getTime());
        // 转换hex编码
        for (byte b : bytes) {

            String tmp = Integer.toHexString(b + btRan[random.nextInt(btRan.length)]);
            sb.append(tmp);
        }
        str = String.format("%s%s%s", base64Ran[random.nextInt(base64Ran.length)],
                Base64.encodeBase64String((sb.toString()).getBytes()), base64Ran[random.nextInt(base64Ran.length)]);
        return str;
    }


    /**
     * 用指定字符格式化字符串
     *
     * @param old
     *            原字符串
     * @param sub
     *            指定字符
     * @param len
     *            格式化后字符串总长度
     * @param isAddAfter
     *            为true则在后面添加，否则添加到前面
     * @param isCutMore
     *            是否截断超出指定长度部分的字符串
     * @return
     */
    public static String FormatStrLen(String old, String sub, int len, boolean isAddAfter, boolean isCutMore) {
        StringBuilder sb = new StringBuilder();
        if (null == old) {
            old = "";
        }
        if (null == sub) {
            sub = "0";
        }
        int addLen = len - old.length();
        for (int i = 0; i < addLen; i++) {
            sb.append(sub);
        }
        if (isAddAfter) {
            sb.insert(0, old);
        } else {
            sb.append(old);
        }
        if (isCutMore) {
            return sb.toString().substring(0, len);
        } else {
            return sb.toString();
        }
    }
}
